package com.neu.ui.dashboard;

import android.app.DownloadManager;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.bean.Category;
import com.neu.bean.News;
import com.neu.util.VolleyUtil;

public class DashboardViewModel extends ViewModel {
    //新闻分类
    private MutableLiveData<Category> mCategory;
    private MutableLiveData<News> mNews;
    private Gson gson;

    public DashboardViewModel(){
        mCategory=new MutableLiveData<>();
        mNews=new MutableLiveData<>();
        gson=new Gson();
    }

    //返回新闻信息
    public LiveData<News> getNews(){
        return mNews;
    }
    //根据类型获得新闻
    public void loadNews(String type){
        String url="/prod-api/press/press/list?type="+type;
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                News news=gson.fromJson(json,News.class);
                mNews.setValue(news);
            }
        });
    }

    //获得新闻分类
    public LiveData<Category> getCategory(){
        String url="/prod-api/press/category/list";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Category category=gson.fromJson(json,Category.class);
                mCategory.setValue(category);
            }
        });
        return  mCategory;
    }
}